package com.feihua.starter.web.modules.hdb.mvc;

import com.feihua.framework.rest.ResponseJsonRender;
import com.feihua.framework.rest.interceptor.RepeatFormValidator;
import com.feihua.framework.rest.modules.common.mvc.BaseController;
import com.feihua.starter.service.api.ApiHdbTemplatePoService;
import com.feihua.starter.service.modules.hdb.dto.HdbTemplateDto;
import com.feihua.starter.service.modules.hdb.dto.SearchHdbTemplatesConditionDto;
import com.feihua.starter.service.modules.hdb.po.HdbTemplatePo;
import com.feihua.utils.http.httpServletResponse.ResponseCode;
import feihua.jdbc.api.pojo.PageAndOrderbyParamDto;
import feihua.jdbc.api.pojo.PageResultDto;
import feihua.jdbc.api.utils.OrderbyUtils;
import feihua.jdbc.api.utils.PageUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
/**
 * 活动吧模板管理
 * Created by revolver
 */
@RestController
@RequestMapping("/hdb")
public class HdbTemplateController extends BaseController {

    private static Logger logger = LoggerFactory.getLogger(HdbTemplateController.class);

    @Autowired
    private ApiHdbTemplatePoService apiHdbTemplatePoService;

    /**
     * 单资源，添加
     * @param hdbTemplateDto
     * @return
     */
    @RepeatFormValidator
    @RequiresPermissions("hdb:template:add")
    @RequestMapping(value = "/template",method = RequestMethod.POST)
    public ResponseEntity add(HdbTemplateDto hdbTemplateDto){
        logger.info("添加活动吧模板开始");
        logger.info("当前登录用户id:{}",getLoginUser().getId());
        ResponseJsonRender resultData=new ResponseJsonRender();
        // 表单值设置
        HdbTemplatePo basePo = new HdbTemplatePo();
        BeanUtils.copyProperties(hdbTemplateDto,basePo);

        apiHdbTemplatePoService.preInsert(basePo,getLoginUser().getId());
        HdbTemplateDto r = apiHdbTemplatePoService.insert(basePo);
        if (r == null) {
            // 添加失败
            resultData.setCode(ResponseCode.E404_100001.getCode());
            resultData.setMsg(ResponseCode.E404_100001.getMsg());
            logger.info("code:{},msg:{}",resultData.getCode(),resultData.getMsg());
            logger.info("添加活动吧模板结束，失败");
            return new ResponseEntity(resultData,HttpStatus.NOT_FOUND);
        }else{
            // 添加成功，返回添加的数据
            resultData.setData(r);
            logger.info("添加活动吧模板id:{}",r.getId());
            logger.info("添加活动吧模板结束，成功");
            return new ResponseEntity(resultData, HttpStatus.CREATED);
        }
    }

    /**
     * 单资源，删除
     * @param id
     * @return
     */
    @RepeatFormValidator
    @RequiresPermissions("hdb:template:delete")
    @RequestMapping(value = "/template/{id}",method = RequestMethod.DELETE)
    public ResponseEntity delete(@PathVariable String id){
        logger.info("删除活动吧模板开始");
        logger.info("用户id:{}",getLoginUser().getId());
        logger.info("活动吧模板id:{}",id);
        ResponseJsonRender resultData=new ResponseJsonRender();

            int r = apiHdbTemplatePoService.deleteFlagByPrimaryKeyWithUpdateUser(id,getLoginUser().getId());
            if (r <= 0) {
                // 删除失败，可能没有找到资源
                resultData.setCode(ResponseCode.E404_100001.getCode());
                resultData.setMsg(ResponseCode.E404_100001.getMsg());
                logger.info("code:{},msg:{}",resultData.getCode(),resultData.getMsg());
                logger.info("删除活动吧模板结束，失败");
                return new ResponseEntity(resultData,HttpStatus.NOT_FOUND);
            }else{
                // 删除成功
                logger.info("删除的活动吧模板id:{}",id);
                logger.info("删除活动吧模板结束，成功");
                return new ResponseEntity(resultData,HttpStatus.NO_CONTENT);
            }
    }

    /**
     * 单资源，更新
     * @param hdbTemplateDto
     * @return
     */
    @RepeatFormValidator
    @RequiresPermissions("hdb:template:update")
    @RequestMapping(value = "/template/{id}",method = RequestMethod.PUT)
    public ResponseEntity update(@RequestBody HdbTemplateDto hdbTemplateDto){
        logger.info("更新活动吧模板开始");
        logger.info("当前登录用户id:{}",getLoginUser().getId());
        logger.info("活动吧模板id:{}",hdbTemplateDto.getId());
        ResponseJsonRender resultData=new ResponseJsonRender();
        // 表单值设置
        HdbTemplatePo basePo = new HdbTemplatePo();
        BeanUtils.copyProperties(hdbTemplateDto,basePo);

        int r = apiHdbTemplatePoService.updateByPrimaryKeySelective(basePo);
        if (r <= 0) {
            // 更新失败，资源不存在
            resultData.setCode(ResponseCode.E404_100001.getCode());
            resultData.setMsg(ResponseCode.E404_100001.getMsg());
            logger.info("code:{},msg:{}",resultData.getCode(),resultData.getMsg());
            logger.info("更新活动吧模板结束，失败");
            return new ResponseEntity(resultData,HttpStatus.NOT_FOUND);
        }else{
            // 更新成功，已被成功创建
            logger.info("更新的活动吧模板id:{}",hdbTemplateDto.getId());
            logger.info("更新活动吧模板结束，成功");
            return new ResponseEntity(resultData, HttpStatus.CREATED);
        }
    }

    /**
     * 单资源，获取id活动吧模板
     * @param id
     * @return
     */
    @RepeatFormValidator
    @RequiresPermissions("hdb:template:getById")
    @RequestMapping(value = "/template/{id}",method = RequestMethod.GET)
    public ResponseEntity getById(@PathVariable String id){

        ResponseJsonRender resultData=new ResponseJsonRender();
        HdbTemplateDto baseDataScopeDto = apiHdbTemplatePoService.selectByPrimaryKey(id,false);
        if(baseDataScopeDto != null){
            resultData.setData(baseDataScopeDto);
            return new ResponseEntity(resultData, HttpStatus.OK);
        }else{
            resultData.setCode(ResponseCode.E404_100001.getCode());
            resultData.setMsg(ResponseCode.E404_100001.getMsg());
            return new ResponseEntity(resultData,HttpStatus.NOT_FOUND);
        }
    }

    /**
     * 复数资源，搜索活动吧模板
     * @param dto
     * @return
     */
    @RepeatFormValidator
    @RequiresPermissions("hdb:template:search")
    @RequestMapping(value = "/templates",method = RequestMethod.GET)
    public ResponseEntity search(SearchHdbTemplatesConditionDto dto){

        ResponseJsonRender resultData=new ResponseJsonRender();
        PageAndOrderbyParamDto pageAndOrderbyParamDto = new PageAndOrderbyParamDto(PageUtils.getPageFromThreadLocal(), OrderbyUtils.getOrderbyFromThreadLocal());
        // 设置当前登录用户id
        PageResultDto<HdbTemplateDto> list = apiHdbTemplatePoService.searchHdbTemplatesDsf(dto,pageAndOrderbyParamDto);

        if(CollectionUtils.isNotEmpty(list.getData())){
            resultData.setData(list.getData());
            resultData.setPage(list.getPage());
            return new ResponseEntity(resultData, HttpStatus.OK);
        }else{
            resultData.setCode(ResponseCode.E404_100001.getCode());
            resultData.setMsg(ResponseCode.E404_100001.getMsg());
            return new ResponseEntity(resultData,HttpStatus.NOT_FOUND);
        }
    }
}
